package com.aprivate.kn.ratingsystem.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aprivate.kn.ratingsystem.R;
import com.aprivate.kn.ratingsystem.daoBean.Elements;
import com.aprivate.kn.ratingsystem.util.CommonUtil;

import java.util.List;

/**
 * Created by zhangshaokun on 2018/12/16.
 */

public class RatingClassAdapter extends android.widget.BaseAdapter {
    private Context mContext;
    private List<Elements> mData;
    private int mCurrentItem = 0;

    public RatingClassAdapter(Context context, List<Elements> data) {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Elements getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_question, null);
            viewHolder.mQuestionText = view.findViewById(R.id.text_question_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = CommonUtil.dp2Px(mContext, 8);
        lp.setMargins(margin, 0, margin, 0);
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        viewHolder.mQuestionText.setLayoutParams(lp);
        viewHolder.mQuestionText.setPadding(margin, margin, margin, margin);
        viewHolder.mQuestionText.setTag(i);
        viewHolder.mQuestionText.setGravity(Gravity.CENTER);
        viewHolder.mQuestionText.setTextSize(15);
        if (mCurrentItem == i) {
            viewHolder.mQuestionText.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            viewHolder.mQuestionText.setTextColor(ContextCompat.getColor(mContext, R.color.background_gradient_start));
        }
        if (null != getItem(i) && !TextUtils.isEmpty(getItem(i).getContext())) {
            viewHolder.mQuestionText.setText(getItem(i).getContext());
        }
        return view;
    }

    public void setCurrentItem(int position) {
        this.mCurrentItem = position;
        notifyDataSetChanged();
    }

    public int getCurrentItem(){
        return mCurrentItem;
    }


    public static class ViewHolder {
        public TextView mQuestionText;
    }
}
